package com.example.crm.controller;

import com.example.crm.entity.User;
import com.example.crm.service.UserService;
import com.example.crm.util.ResBean;
import com.example.crm.util.ResBeanUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
public class login {
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private RedisTemplate redisTemplate;

    private Jedis jedis;
    @Before
    public void before(){
        jedis = new Jedis("127.0.0.1", 6379);
//        jedis.auth("foobared");
    }

    @RequestMapping("/logout")
    public String loginOut(HttpSession session){
        session.removeAttribute("user");
        SecurityUtils.getSubject().logout();
        return "login";
    }

    @RequestMapping("/main")
    public String main(){
        return "main";
    }

    @RequestMapping(value = "/doLogin")
    @ResponseBody
    public ResBean login(Model model, String name, String pwd, HttpSession session,Map map){
        ValueOperations<String,String> operations = redisTemplate.opsForValue();
        Integer count = stringRedisTemplate.opsForValue().get("login")==null?0:Integer.valueOf(stringRedisTemplate.opsForValue().get("login"));
        System.out.println(name);
        if (!name.equals(stringRedisTemplate.opsForValue().get("id"))) {
            //通过shiro实现认证
            UsernamePasswordToken token = new UsernamePasswordToken(name, pwd);
            Subject subject = SecurityUtils.getSubject();
            try {
                subject.login(token); //调用shiro认证
                //保存登入的信息==>通过shiro中认证的身份
                User loginUser = (User) subject.getPrincipal();
            List<User> list = userService.finAll();
            session.setAttribute("userList", list);
            session.setAttribute("user", loginUser);
            stringRedisTemplate.delete("login");
            map.put("name","登入成功！");
            map.put("time",stringRedisTemplate.getExpire("id"));
            return ResBeanUtil.success(map);
            }catch (UnknownAccountException e){
                model.addAttribute("message", "账号或密码错误！");
                count = count + 1;
                map.put("name","账号或密码错误！");
                map.put("time",stringRedisTemplate.getExpire("id"));
                if (count == 3) {
                    stringRedisTemplate.delete("login");
                    stringRedisTemplate.opsForValue().set("id",name,60*10, TimeUnit.SECONDS);
//                    operations.set("id", user.getUsr_name());
                    map.put("name","连续三次输入错误，此账号要冻结10分钟！");
                    map.put("time",stringRedisTemplate.getExpire("id"));
                }
                stringRedisTemplate.opsForValue().set("login", String.valueOf(count));
                return ResBeanUtil.error(map);
            }catch (LockedAccountException e1){
                  System.out.println("该账号已被锁定");
            map.put("name","该账号已被锁定！");
            map.put("time","-1");
            return  ResBeanUtil.error(map);
            }
//            User user = userService.findNameAndPassword(name, pwd);
//            List<User> list = userService.finAll();
//            if (user != null) {
//                session.setAttribute("user", user);
//                session.setAttribute("userList", list);
//                stringRedisTemplate.delete("login");
//                map.put("name","登入成功！");
//                map.put("time",stringRedisTemplate.getExpire("id"));
//                    return ResBeanUtil.success(map);
//            } else {
//                model.addAttribute("message", "账号或密码错误！");
//                count = count + 1;
//                map.put("name","账号或密码错误！");
//                map.put("time",stringRedisTemplate.getExpire("id"));
//                if (count == 3) {
//                    stringRedisTemplate.delete("login");
//                    stringRedisTemplate.opsForValue().set("id",name,60*10, TimeUnit.SECONDS);
////                    operations.set("id", user.getUsr_name());
//                    map.put("name","连续三次输入错误，此账号要冻结10分钟！");
//                    map.put("time",stringRedisTemplate.getExpire("id"));
//                }
//                stringRedisTemplate.opsForValue().set("login", String.valueOf(count));
//                return ResBeanUtil.error(map);
//            }
        }else {
            System.out.println("该账号已冻结，请"+stringRedisTemplate.getExpire("id")+"后进行登入！");
            map.put("name","该账号已冻结！");
            map.put("time",stringRedisTemplate.getExpire("id"));
            return  ResBeanUtil.error(map);
        }

        }
}
